/*
 * @Author: fisher
 * @Date: 2024-01-15 17:50:41
 * @LastEditTime: 2024-02-13 16:08:12
 * @LastEditors: fisher luthor-li@qq.com
 * @Description:
 * @FilePath: \allfront\src\utils\cesium\common.js
 */
import { imageMap } from './cesiumResource.js';
import { shenzhenView } from './config.js';
export const initMap = () => {
  Cesium.Ion.defaultAccessToken =
    'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiI4NjQwZGMwZi1lYmJjLTQ3NDEtOWNlOS0xN2Q1OWQxYTA2NGUiLCJpZCI6OTA3NjQsImlhdCI6MTY1MDU1MjI5MH0.MkFGfYbFiVIZj0R1gsDA00RjEK-Hf3rux33QQF5MZ0M';
  // 初始化场景
  window.viewer = new Cesium.Viewer('cesiumMap', {
    // 去掉默认点选弹窗
    infoBox: false,
    // 去掉左下角动画播放按钮
    animation: false,
    // 去掉右上角搜索按钮
    geocoder: false,
    // 去掉右上角选择底图按钮
    baseLayerPicker: false,
    // 去掉右上角二三维模式切换按钮
    sceneModePicker: false,
    // 去掉右上角返回主视角按钮
    homeButton: false,
    // 去掉右上角帮助说明按钮
    navigationHelpButton: false,
    // 去掉底部时间线
    timeline: false,
    //   去掉右下角全屏按钮
    fullscreenButton: false,
  });
  //    去掉底部版本信息
  viewer.bottomContainer.style.display = 'none';
  //   添加深度检测
  viewer.scene.globe.depthTestAgainstTerrain = true;
  //    添加天地图底图
  viewer.imageryLayers.addImageryProvider(imageMap);
  //   添加帧数检测
  viewer.scene.debugShowFramesPerSecond = true;

  //   viewer.clock.currentTime = Cesium.JulianDate.fromDate(
  //     new Date("2023-11-09T12:00:00")
  //   );

  //   var tmsLayer = viewer.scene.imageryLayers.addImageryProvider(
  //     new Cesium.TileMapServiceImageryProvider({
  //       url: new Cesium.Resource({
  //         url: "http://10.253.66.26/gw/OGC/Map/SZ_IMGTDOM02_2019/{z}/{x}/{y}",
  //         // url: 'http://10.253.102.69/gw/OGC/Map/SZ_IMGTDOM02_2019/{z}/{x}/{y}',
  //         headers: {
  //           "szvsud-license-key":
  //             "jFZh3bYf+NGoRA22OiF3M1gHGIaCKx9NFfFueD7jjMsleqXXz/H25dHhpk4SCw6f",
  //         },
  //       }),
  //     })
  //   );

  // 设置初始视角
  setView(shenzhenView);

  //   const helper = new Cesium.EventHelper();
  //   const res = helper.add(viewer.scene.globe.tileLoadProgressEvent, (e) => {
  //     console.log("load e = ", e);
  //   });
  //   console.log("helper res =", res);
  return viewer;
};

/**
 * @description: 设置镜头操作
 * @param {Object} viewData
 * @param {Number} duration
 * @return {*}
 */
export const setView = (viewData, duration = 1.5) => {
  viewer.scene.camera.flyTo({
    destination: new Cesium.Cartesian3(viewData.x, viewData.y, viewData.z),
    orientation: {
      heading: viewData.heading,
      pitch: viewData.pitch,
      roll: viewData.roll,
    },
    duration: duration,
  });
};
/**
 * @description: 视角设置到对象身上
 * @param {*} obj
 * @return {*}
 */
export const setView2Obj = obj => {
  viewer.flyTo(obj);
};

/**
 * @description: 开启图层加载监听
 * @param {*} cb
 * @return {*}
 */
export const loadTilesAction = cb => {
  const helper = new Cesium.EventHelper();
  helper.add(viewer.scene.globe.tileLoadProgressEvent, cb);
  return helper;
};
/**
 * @description: 清除图层加载监听
 * @param {*} helper
 * @return {*}
 */
export const removeLoadTilesAction = helper => {
  helper && helper.removeAll();
  console.log('helper1', helper);
  helper = undefined;
  console.log('helper2', helper);
};
